#!/bin/bash

# Update the system
dnf -y update

# Install basic packages
dnf -y install git zsh htop ncdu wget unzip

# Install Oh My Zsh and plugins
git clone https://gitee.com/walter_bishop/ohmyzsh.git
./ohmyzsh/tools/install.sh

plugins_dir=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins
git clone https://gitee.com/walter_bishop/zsh-autosuggestions.git "$plugins_dir/zsh-autosuggestions"
git clone https://gitee.com/walter_bishop/zsh-syntax-highlighting.git "$plugins_dir/zsh-syntax-highlighting"

# Configure Oh My Zsh
sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting)/' ~/.zshrc
source ~/.zshrc

# Install and configure Nginx
dnf -y install yum-utils
cat <<EOL > /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOL

dnf -y install nginx
systemctl start nginx
systemctl enable nginx

# Create necessary directories and symbolic links for Nginx
mkdir -p /data/nginx/logs.d /data/projects /etc/nginx/cert.d
ln -s /etc/nginx/conf.d /data/nginx/
ln -s /etc/nginx/cert.d /data/nginx/

# Install and configure Redis
dnf -y install redis
systemctl start redis
systemctl enable redis

# Install and configure PHP
dnf -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-9.rpm
dnf -y install php82

# Create symbolic link for PHP
ln -s /usr/bin/php82 /usr/bin/php

# Install necessary PHP extensions
php_extensions=("bcmath" "fpm" "gd" "mbstring" "mysqli" "mysqlnd" "PDO" "pdo_mysql" "pdo_sqlite" "posix" "redis" "shmop" "soap" "sodium" "sqlite3" "sysvmsg" "sysvsem" "sysvshm" "dom" "SimpleXML" "xml" "xmlreader" "xmlwriter" "xsl" "zip")

dnf -y install "${php_extensions[@]/#/php82-php-}"

# Start and enable PHP-FPM
systemctl start php82-php-fpm
systemctl enable php82-php-fpm

# Install Composer
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"

# Generate SSH key and display public key
ssh-keygen -t rsa -C "$(hostname)" -f ~/.ssh/id_rsa -N ""
cat ~/.ssh/id_rsa.pub

echo "脚本执行完成！"